<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">销售订单列表</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="SKU名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入SKU名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="审核状态" prop="approveStatus" >
          <el-select
            v-model="queryParams.approveStatus"
            placeholder="全部"
            clearable
            size="small"
            style="width:100px;"
          >
            <el-option
              v-for="item in approveStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="按起止时间" prop="time">
          <el-date-picker
              v-model="time"
              type="daterange"
              range-separator="至"
              style="width: 230px;"
              @change="dataTime()"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作人" prop="updateBy">
          <el-input
            v-model="queryParams.updateBy"
            placeholder="请输入操作人"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            class="btn-success"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="orderAdd()"
            v-hasPermi="['system:salesOrder:add']"
          >新建</el-button>
        </el-col>
        <el-col :span="21">
          <el-button
            class="btn-scanCodeDel"
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="btnExport()"
          >导出</el-button>
        </el-col>
        <el-col :span="1">
          <el-popover placement="bottom" title="筛选列" trigger="click" width="40">
            <el-checkbox-group v-model="checkedColumns" size="mini">
              <el-checkbox v-for="item in checkBoxGroup" :key="item" :label="item" :value="item"></el-checkbox>
            </el-checkbox-group>
            <div title="筛选列" class="filter-table-col" slot="reference">
              <img src="@/assets/img/screening.png" width="25" />
            </div>
          </el-popover>
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="orderList" @row-click="handleView" @selection-change="handleSelectionChange" height="calc(100vh - 350px)" :key="reload" border>
        <el-table-column type="selection" width="35" align="center" />
        <el-table-column label="序号" type="index" align="center" width="50">
          <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <template v-for="(item, index) in tableHeaderData">
          <el-table-column
            :label="item.label"
            :prop="item.prop"
            :align="item.align"
            :sortable="item.sortable"
            :show-overflow-tooltip="true"
            :key="index"
            :width="item.width"
            v-if="colData[index].isTrue"
          >
            <template slot-scope="scope">
              <template v-if="item.label == '审核状态'">
                <div v-if="scope.row.approveStatus == 0" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.approveStatus == 2" style="color:#13C2C2">反审核</div>
                <div v-if="scope.row.approveStatus == 3" style="color:#fd562c">拒绝</div>
                <div v-if="scope.row.approveStatus == null" style="color:#13C2C2">待编辑</div>
              </template>
              <template v-else-if="item.label == '产品编码'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.productBasicCode }}
                </div>
              </template>
              <template v-else-if="item.label == 'SKU编码'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.productCode }}
                </div>
              </template>
              <template v-else-if="item.label == '产品名称'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.productBasicName }}
                </div>
              </template>
              <template v-else-if="item.label == '规格'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.productSpace }}
                </div>
              </template>
              <template v-else-if="item.label == '批次'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.batchNumber }}
                </div>
              </template>
              <template v-else-if="item.label == '单位'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.unit }}
                </div>
              </template>
              <template v-else-if="item.label == '数量'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.total }}
                </div>
              </template>
              <template v-else-if="item.label == '单价'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.unitPrice }}
                </div>
              </template>
              <template v-else-if="item.label == '金额'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.totalPrice }}
                </div>
              </template>
              <template v-else-if="item.label == '含税单价'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.taxUnitPrice }}
                </div>
              </template>
              <template v-else-if="item.label == '价税合计'">
                <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
                  {{ item.totalPriceTax }}
                </div>
              </template>
              <template v-else-if="item.label == '状态'">
                <div v-if="scope.row.status == 1" style="color:#13C2C2">待审核</div>
                <div v-if="scope.row.status == 2" style="color:#52C41A">已审核</div>
                <div v-if="scope.row.status == 3" style="color:#52C41A">已完成</div>
                <div v-if="scope.row.status == 4" style="color:#52C41A">部分出库</div>
                <div v-if="scope.row.status == 9" style="color:#52C41A">自动</div>
                <div v-if="scope.row.status == 0" style="color:#13C2C2">草稿</div>
              </template>
              <span v-else>{{ scope.row[scope.column.property] }}</span>
            </template>
          </el-table-column>
        </template>
        <!-- <el-table-column v-if="colData[0].isTrue" label="订单编码"  align="center" prop="orderCode" width="150px" />
        <el-table-column v-if="colData[1].isTrue" label="客户名称"  align="center" prop="customerName" :show-overflow-tooltip="true" width="150px" />
        <el-table-column v-if="colData[2].isTrue" label="客户部门"  align="center" prop="customerDept" :show-overflow-tooltip="true" width="100px" />
        <el-table-column v-if="colData[3].isTrue" label="审核状态" align="center" prop="approveStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.approveStatus == 0" style="color:#13C2C2">待审核</div>
            <div v-if="scope.row.approveStatus == 1" style="color:#52C41A">已审核</div>
            <div v-if="scope.row.approveStatus == 2" style="color:#13C2C2">反审核</div>
            <div v-if="scope.row.approveStatus == 3" style="color:#fd562c">拒绝</div>
            <div v-if="scope.row.approveStatus == null" style="color:#13C2C2">待编辑</div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[4].isTrue" label="产品编码" align="center" prop="productCode" :show-overflow-tooltip="true" width="150px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.productBasicCode }}
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column v-if="colData[4].isTrue" label="SKU编码" align="center" prop="productCode" :show-overflow-tooltip="true" width="150px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.productCode }}
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column v-if="colData[5].isTrue" label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true" width="200px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.productBasicName }}
            </div>
          </template>
        </el-table-column> -->
<!--        <el-table-column v-if="colData[5].isTrue" label="SKU名称" align="center" prop="productName" :show-overflow-tooltip="true" width="200px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.productName }}
            </div>
          </template>
        </el-table-column>-->
        <!-- <el-table-column v-if="colData[6].isTrue" label="规格" align="center" prop="productSpace" :show-overflow-tooltip="true" width="120px">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.productSpace }}
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column v-if="colData[7].isTrue" label="单位" align="center" prop="unit" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.unit }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[8].isTrue" label="数量" align="center" prop="total" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.total }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[9].isTrue" label="单价" align="center" prop="unitPrice" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.unitPrice }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[10].isTrue" label="金额" align="center" prop="totalPrice" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.totalPrice }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[11].isTrue" label="含税单价" align="center" prop="taxUnitPrice" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.taxUnitPrice }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[12].isTrue" label="价税合计" align="center" prop="totalPriceTax" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.salesOrderDetailList" :key="index" class="item">
              {{ item.totalPriceTax }}
            </div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[13].isTrue" label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1" style="color:#13C2C2">待审核</div>
            <div v-if="scope.row.status == 2" style="color:#52C41A">已审核</div>
            <div v-if="scope.row.status == 3" style="color:#52C41A">已完成</div>
            <div v-if="scope.row.status == 4" style="color:#52C41A">部分出库</div>
            <div v-if="scope.row.status == 9" style="color:#52C41A">自动</div>
            <div v-if="scope.row.status == 0" style="color:#13C2C2">草稿</div>
          </template>
        </el-table-column>
        <el-table-column v-if="colData[14].isTrue" label="操作人" align="center" prop="updateBy" :show-overflow-tooltip="true" />
        <el-table-column v-if="colData[15].isTrue" label="备注" align="center" prop="remark" :show-overflow-tooltip="true" /> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click.native.stop="handleDelete(scope.row)"
              v-hasPermi="['system:salesOrder:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { orderList, orderDelete } from "@/api/system/order/order"
import { exportSalesOrder } from "@/api/system/export/export"

export default {
  name: "order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 列表数据
      orderList: [],
      //审核状态
      approveStatus:[
        {
          value:'0',
          label:'待审核'
        },
        {
          value:'1',
          label:'已审核'
        },
        {
          value:'2',
          label:'反审核'
        },
        {
          value:'3',
          label:'拒绝'
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: undefined, // 客户名称
        productName: undefined,
        //审核状态（0待审核 1已审核 2 未通过(反审核)3拒绝）
        approveStatus: undefined,
        updateBy: undefined,
        startTime: '',
        endTime: ''
      },
      ids: [],
      time: [],
      reload: '',
      tableHeaderData: [
        { label: "订单编码", align: "center", prop: "orderCode", width: "150" },
        { label: "客户名称", align: "center", prop: 'customerName', width: "150" },
        { label: "客户部门", align: "center", prop: "customerDept", width: "100" },
        { label: "审核状态", align: "center", prop: "approvalStatusName"},
        { label: "产品编码", align: "center", prop: "productBasicCode", width: "150" },
        { label: "SKU编码", align: "center", prop: "productCode", width: "150" },
        { label: "产品名称", align: "center", prop: "productBasicName", width: "150" },
        { label: "规格", align: "center", prop: "productSpace", width: "120" },
        { label: "批次", align: "center", prop: "batchNumber", width: "100" },
        { label: "单位", align: "center", prop: "unit" },
        { label: "数量", align: "center", prop: "total" },
        { label: "单价", align: "center", prop: "unitPrice" },
        { label: "金额", align: "center", prop: "totalPrice" },
        { label: "含税单价", align: "center", prop: "taxUnitPrice" },
        { label: "价税合计", align: "center", prop: "totalPriceTax" },
        { label: "状态", align: "center", prop: "statusName" },
        { label: "操作人", align: "center", prop: "updateBy" },
        { label: "备注", align: "center", prop: "remark" },
      ],
      // colData中列出表格中的每一列，默认都展示
		  colData: [
        { title: "订单编码", isTrue: true },
        { title: "客户名称", isTrue: true },
        { title: "客户部门", isTrue: true },
        { title: "审核状态", isTrue: true },
        { title: "产品编码", isTrue: true },
        { title: "SKU编码", isTrue: true },
        { title: "产品名称", isTrue: true },
        { title: "规格", isTrue: true },
        { title: "批次", isTrue: true },
        { title: "单位", isTrue: true },
        { title: "数量", isTrue: true },
        { title: "单价", isTrue: true },
        { title: "金额", isTrue: true },
        { title: "含税单价", isTrue: true },
        { title: "价税合计", isTrue: true },
        { title: "状态", isTrue: true },
        { title: "操作人", isTrue: true },
        { title: "备注", isTrue: true },
      ],
      // 当前选中的多选框，代表当前展示的列
      checkedColumns: ["订单编码", "客户名称", "客户部门", "审核状态", "产品编码", "SKU编码", "产品名称", "规格", "批次", "单位", "数量", "单价", "金额", "含税单价", "价税合计", "状态", "操作人", "备注"],
      // 多选框的列表，列出表格的每一列
      checkBoxGroup: ["订单编码", "客户名称", "客户部门", "审核状态", "产品编码", "SKU编码", "产品名称", "规格", "批次", "单位", "数量", "单价", "金额", "含税单价", "价税合计", "状态", "操作人", "备注"]
    }
  },
  created() {
    this.getList()
  },
  watch: {
    checkedColumns(val) {
      let arr = this.checkBoxGroup.filter(i => !val.includes(i));
      this.colData.filter(i => {
        if (arr.indexOf(i.title) != -1) {
          i.isTrue = false;
        } else {
          i.isTrue = true;
        }
      });
      this.reload = Math.random()
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      orderList(this.addDateRange(this.queryParams)).then(res => {
          this.orderList = res.rows
          this.total = res.total
          this.loading = false
        }
      )
    },
    // 搜索按钮
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.resetForm("queryForm")
      this.time = ''
      this.queryParams.startTime = ''
      this.queryParams.endTime = ''
      this.handleQuery()
    },
    // 新建
    orderAdd(){
      this.$router.push({path:"/sales/salesOrder/add"})
    },
    // 查看详情
    handleView(row) {
      this.$router.push({path:"/sales/salesOrder/detail", query:{id: row.id}})
    },
    // 删除
    handleDelete(row){
      this.$modal.confirm('是否确认删除？').then(() => {
        orderDelete(row.id).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        })
      }).catch(() => {})
    },
    // 时间选择器
    dataTime(){
      if(this.time){
        let day = 24 * 60 * 60 * 1000
        this.queryParams.startTime = this.parseTime(this.time[0])
        this.queryParams.endTime = this.parseTime(this.time[1].getTime() + day)
      }else if(this.time == null){
        this.queryParams.startTime = ''
        this.queryParams.endTime = ''
      }
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.map(item => item.id)
    },
    // 导出
    btnExport(){
      let a = this.colData.filter(i => {
        return i.isTrue == true
      })
      let b = []
      a.forEach((item) => {
        this.tableHeaderData.forEach((data) => {
          if(item.title == data.label){
            b.push({name: data.prop, value: item.title})
          }
        })
      })
      const obj = b.reduce((result, item) => {
        result[item.name] = item.value
        return result
      }, {})
      if(this.ids.length > 0){
        let data = {
          ids: this.ids,
          headMap: obj,
          customerName: this.queryParams.customerName,
          productName: this.queryParams.productName,
          approveStatus: this.queryParams.approveStatus,
          updateBy: this.queryParams.updateBy,
          startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
          endDate: this.queryParams.endTime ? this.queryParams.endTime : undefined
        }
        this.getExport(data)
      }else{
        let data = {
          headMap: obj,
          customerName: this.queryParams.customerName,
          productName: this.queryParams.productName,
          approveStatus: this.queryParams.approveStatus,
          updateBy: this.queryParams.updateBy,
          startTime: this.queryParams.startTime ? this.queryParams.startTime : undefined,
          endDate: this.queryParams.endTime ? this.queryParams.endTime : undefined
        }
        this.getExport(data)
      }
    },
    getExport(data){
      this.$modal.loading("正在导出")
      exportSalesOrder(data).then(res => {
        this.$modal.closeLoading()
        let blob = new Blob([res], {
          type: res.type,
        })
        let downloadElement = document.createElement("a")
        let href = window.URL.createObjectURL(blob)
        downloadElement.href = href
        downloadElement.download = "销售订单.xls"
        document.body.appendChild(downloadElement)
        downloadElement.click()
        window.URL.revokeObjectURL(href)
      })
    }
  }
}
</script>

<style scoped>
.filter-table-col .el-icon-c-scale-to-original{
  font-size: 20px;
  cursor: pointer;
}
::v-deep .el-table__body .el-table__cell{
  padding:0;
}
::v-deep .el-table__body .cell{
  padding: 10px;
}
.item{
  height: 25px;
  margin-bottom: 10px;
}
.item:last-child{
  margin-bottom: 0;
}
</style>
